home *** CD-ROM | disk | FTP | other *** search
- Path: mujibur.inmind.com!usenet
- From: mfinney@inmind.com
- Newsgroups: comp.lang.c++
- Subject: Re: Doubly Linked List help - please!
- Date: 31 Mar 1996 03:54:51 GMT
- Organization: In Mind, Inc.
- Message-ID: <4jkvmb$dac@mujibur.inmind.com>
- References: <internews46B70D427A@argonet.co.uk>
- Reply-To: mfinney@inmind.com
- NNTP-Posting-Host: finneyman.inmind.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <internews46B70D427A@argonet.co.uk>, Charlotte Tomlinson <eeyore@argonet.co.uk> writes:
- >I have implemented a template doubly linked list, the backbones of which
- >is as follows:
- ..<snip>...
- >template <class T> class dllist : public node<T>
- >{
-
- This is a problem. A dllist<T> is NOT a node<T>. A linked list
- uses zero or more nodes, but is not itself a node. It generally is
- not meaningful to send an instance of dllist the getNext() or the
- getPrev() message. So dllist should not inherit (pubically or
- privately, for that matter) from node<T>.
-
- ..<snip>...
- >Then I have a class, called fuzzyset, which uses this dll as a container
- >for fuzzyel instances:
- ..<snip>...
- >What I would like to do is, within dllist, to implement a function forEach
- >that take a pointer to a function as one argument, and somehow the rest of
- >the arguments required by that function, and carries that function out
- >over each element of the list.
-
- There are probably two things that you need. First, you need an
- "iterator" class, say dllistIterator, which is a friend of dllist. This
- class should contain a pointer to an instance of dllist and know
- enough about the structure of dllist that it can follow the linked
- list. Generally it needs to be able to be reset, to be advanced,
- to be retreated, to obtain the element at the cursor and so forth.
- The dliterator class should inherit from a general abstract iterator
- class.
-
- Then, the fuzzyset class needs a function which returns an instance
- of an iterator over itself. This allows the representation of fuzzyset
- to be changed later, say to a binary tree. Then, of course, it would
- return an instance of treeIterator instead of dllistIterator.
-
- This allows the user of an fuzzyset instance to create any number of
- iterators and to "iterate" over its contents.
-
- Second, you need a "functor" class which overloads operator() for
- objects of type T. You can provide common functors for the user,
- but the user can also create a new subclass of functor to specialized
- purposed. Then you can add a function to fuzzyset which will accept
- an instance of a subclass of functor and call it with each element of
- fuzzyset. Since such an instance is an object, it can contain any data
- whatsoever -- and therefore it can contain the other parameters to
- the function you desire. And operator() effectively implements that
- function. Thus the fuzzyset class has no knowledge of the function
- and so provides a completely generic solution.
-
- Example:
-
- template <class T> class Functor
- {
- void operator()(T const & aT);
- };
-
- template <class T> class ElementCount : public Functor
- {
- private:
- unsigned long count;
- public:
- void operator()(T const & aT) {++count;}
- unsigned long elements() {return count;};
- ElementCount() {count = 0;};
- ~ElementCount();
- };
-
- fuzzyset aSet;
- ElementCount aCount;
-
- aSet.apply(aCount);
-
- cout << "fuzzyset contains " << aCount.elements() << " elements" << endl;
-
-
- Michael Lee Finney
-
-